import time
from selenium import webdriver
from selenium.webdriver import Keys
from selenium.webdriver.common.by import By
from common.functions import open_browser
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.select import Select


class Base:
    def __init__(self, browser):
        self.driver = browser
        self.driver.maximize_window()

    # 获取网址
    def base_get(self, url):
        try:
            self.driver.get(url)
        except Exception as e:
            print('输入网址不正确')

    # 切换iframe
    def base_switch_iframe(self, locator):
        try:
            iframe = self.base_find_element(locator)
            self.driver.switch_to.frame(iframe)
        except Exception:
            raise Exception('切换iframe错误！')

    # 返回上一级iframe
    def base_switch_parent_iframe(self):
        try:
            self.driver.switch_to.parent_frame()
        except Exception:
            raise Exception('返回上一级iframe错误！')

    # 退出iframe
    def base_switch_default_iframe(self):
        try:
            self.driver.switch_to.default_content()
        except Exception:
            raise Exception('退出iframe错误！')

    # 查找元素
    def base_find_element(self, locator):
        try:
            return WebDriverWait(self.driver, timeout=5).until(EC.presence_of_element_located(locator))
        except Exception:
            raise Exception('元素查找错误！')

    # 查找一列元素
    def base_find_elements(self, locator):
        try:
            return WebDriverWait(self.driver, timeout=5).until(EC.presence_of_all_elements_located(locator))
        except Exception:
            raise Exception('元素查找错误！')

    # 输入
    def base_send_keys(self, locator, str):
        ele = self.base_find_element(locator)
        if ele is not None:
            ele.send_keys(str)
        else:
            print('元素不存在')



    # 获取属性
    def base_get_attribute(self, locator, str):
        ele = self.base_find_element(locator)
        if ele is not None:
            attribute = ele.get_attribute(str)
            return attribute
        else:
            print('元素不存在')

    # 获取文本
    def base_get_element_text(self, locator):
        ele = self.base_find_element(locator)
        if ele:
            return ele.text
        else:
            print('元素不存在')

    # 点击按钮
    def base_click(self, locator):
        ele = self.base_find_element(locator)

        if ele is not None:
            ele.click()
        else:
            print('元素不存在')

    # 前进
    def base_forward(self):
        self.driver.forward()

    # 后退
    def base_back(self):
        self.driver.back()

    # 刷新
    def base_refresh(self):
        self.driver.refresh()

    # 退出
    def base_quit(self, second=2):
        time.sleep(second)
        self.driver.quit()

    # select下拉选
    # 根据值选
    def base_select_value(self, locator, value):
        ele = self.base_find_element(locator)
        if ele is not None:
            select = Select(ele)
            return select.select_by_value(value)
        else:
            print('元素定位失败')

    # 根据文本选
    def base_select_text(self, locator, text):
        ele = self.base_find_element(locator)
        if ele is not None:
            select = Select(ele)
            return select.select_by_visible_text(text)
        else:
            print('元素定位失败')

    # 根据索引选
    def base_select_index(self, locator, index):
        ele = self.base_find_element(locator)
        if ele is not None:
            select = Select(ele)
            return select.select_by_index(index)
        else:
            print('元素定位失败')

    # 5.23
    # 页面函数执行js方法
    def base_execute_script(self, ele):
        try:
            self.driver.execute_script(ele)
        except Exception:
            raise Exception('执行js方法错误！')

    # 清空（clear存在部分不能清空情况,故用ctrl+a）
    def base_select_all(self, locator):
        self.base_send_keys(locator, (Keys.CONTROL, 'a'))

    # 5.24
    # 获取页面title
    def base_get_title(self):
        return self.driver.title

    #  获取弹窗文本信息
    def base_get_alert_text(self):
        alert = self.driver.switch_to.alert
        text = alert.text
        alert.accept()
        return text

    # 弹窗处理
    def base_alert_operation(self):
        time.sleep(2)
        self.driver.switch_to.alert.accept()
       
    # 判断元素是否存在 方法封装
    def base_element_is_exist(self, locator):
        try:
            self.base_find_element(locator)
            return True  # 代表元素存在
        except:
            return False  # 代表元素不存在
			
	# 清空操作
    def base_clear(self, locator):
        # 清空文本
        element = self.base_find_element(locator)
        if element:
            element.clear()



if __name__ == '__main__':
    browser = open_browser('chrome')
